<?php

namespace App\Http\Controllers;

use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

use Illuminate\Support\Facades\DB;
use App\Tools\Func;
use App\Models\User;
use App\Models\Guahao;
use App\Models\GuahaoSet;

class GuahaoController extends Controller
{
	use Func;
	
	// 挂号列表
	public function guahao(Request $request)
	{
		$res = Guahao::with('jiuzhenren', 'guahaoSet')->where('user_id', $request->user_id ?? 1)->orderBy('id', 'desc')->get();

	    return $this->returnJson(0, 'success', $res);
	}
	
	// 获取预约时间段列表
	public function getGuaHaoTimeList(Request $request)
	{
		$res = GuahaoSet::get();
		
		return $this->returnJson(0, 'success', $res);
	}
	
	// 添加挂号
	public function guahaoAdd(Request $request)
	{
		// 查询今天时间段预约人数是否满了
		if (!$guahaoSet = GuahaoSet::where('id', $request->yuyuetime_id)->first()) {
			return $this->returnJson(1, '管理员后台没有设置预约时间段', $guahaoSet);
		}
		$guhaoCount = Guahao::where([
			'yiyuan' => $request->yiyuan,
			'keshi' => $request->keshi,
			'yuyuetime_id' => $request->yuyuetime_id,
			'status' => 0
		])->where('created_at', '>', date('Y-m-d'))->count();
		if ($guhaoCount >= $guahaoSet->nums ) {
			return $this->returnJson(1, '该时间段人数已满', $guhaoCount.$guahaoSet->nums);
		}
		
		if (!$res = Guahao::create([
			'user_id' => $request->user_id ?? 1,
			'yiyuan' => $request->yiyuan,
			'keshi' => $request->keshi,
			'yuyuetime_id' => $request->yuyuetime_id,
			'jiuzhenren_id' => $request->jiuzhenren_id,
			'date_time' => $request->date_time,
			'created_at' => date('Y-m-d H:i:s'),
		])) {
            return $this->returnJson(1, '挂号失败', $res);
        }

        return $this->returnJson(0, '挂号成功', $res);
	}
	
	// 取消预约
	public function clearYuyue(Request $request)
	{
		if (!Guahao::where([
			'user_id' => $request->user_id ?? 1,
			'id' => $request->id
		])->update([
			'status' => 1
		])) {
			 return $this->returnJson(1, '取消失败');
		}
		return $this->returnJson(0, '取消成功');
	}
	
	public function getAdList()
	{
		return $this->returnJson(0, 'success', [
			'https://api.pjrsykyy.cn/img/ad1.png',
			'https://api.pjrsykyy.cn/img/ad2.png',
			'https://api.pjrsykyy.cn/img/ad3.png',
			'https://api.pjrsykyy.cn/img/ad4.png',
		]);
	}
	
	// 挂号基本数据
	public function getData()
	{
	
		$yiyuan = [
			[
				'value' => '盘锦润视眼科医院总院',
				'text' => '盘锦润视眼科医院总院',
			],
			[
				'value' => '大洼润视卓越眼科医院',
				'text' => '大洼润视卓越眼科医院',
			],
			[
				'value' => '润视儿童眼科医院',
				'text' => '润视儿童眼科医院',
			],
			[
				'value' => '润视精致影像体检中心',
				'text' => '润视精致影像体检中心',
			],
		];
		
		$keshi = [
			[
				'value' => '小儿及成人眼科病',
				'text' => '小儿及成人眼科病',
			],
			[
				'value' => '视觉训练',
				'text' => '视觉训练',
			],
			[
				'value' => '中医眼健康管理',
				'text' => '中医眼健康管理',
			],
			[
				'value' => '角膜塑形镜',
				'text' => '角膜塑形镜',
			],
			[
				'value' => '高端（屈光）白内障·老花眼专科',
				'text' => '高端（屈光）白内障·老花眼专科',
			],
			[
				'value' => '青光眼专科',
				'text' => '青光眼专科',
			],
			[
				'value' => '屈光近视手术',
				'text' => '屈光近视手术',
			],
			[
				'value' => '医学影像体检',
				'text' => '医学影像体检',
			],
		];
		
		return $this->returnJson(0, 'success', [
			'yiyuan' => $yiyuan,
			'keshi' => $keshi,
		]);
	}

	

}
